import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../utils/enums/app_enums.dart';

/// 枚举服务类
/// 提供通用的枚举处理和转换方法
class EnumService extends GetxService {
  /// 根据整数值获取设备状态
  DeviceStatus getDeviceStatus(int? value) {
    if (value == null) return DeviceStatus.offline;
    return DeviceStatus.fromValue(value);
  }
  
  /// 根据整数值获取告警级别
  AlertLevel getAlertLevel(int? value) {
    if (value == null) return AlertLevel.info;
    return AlertLevel.fromValue(value);
  }
  
  /// 根据整数值获取告警状态
  AlertStatus getAlertStatus(int? value) {
    if (value == null) return AlertStatus.generated;
    return AlertStatus.fromValue(value);
  }
  
  /// 根据整数值获取产品类型
  ProductType getProductType(int? value) {
    if (value == null) return ProductType.device;
    return ProductType.fromValue(value);
  }
  
  /// 获取设备状态对应的颜色
  Color getDeviceStatusColor(int? value) {
    final status = getDeviceStatus(value);
    switch (status) {
      case DeviceStatus.online:
        return Colors.green;
      case DeviceStatus.offline:
        return Colors.red;
      case DeviceStatus.inactive:
        return Colors.orange;
      case DeviceStatus.disabled:
        return Colors.grey;
    }
  }
  
  /// 获取告警级别对应的颜色
  Color getAlertLevelColor(int? value) {
    final level = getAlertLevel(value);
    return level.color;
  }
  
  /// 获取告警状态文本
  String getAlertStatusText(int? value) {
    final status = getAlertStatus(value);
    return status.displayName;
  }
  
  /// 获取告警状态对应的图标
  IconData getAlertStatusIcon(int? value) {
    final status = getAlertStatus(value);
    return status.icon;
  }
  
  /// 获取产品类型文本
  String getProductTypeText(int? value) {
    final type = getProductType(value);
    return type.displayName;
  }
  
  /// 获取产品类型对应的图标
  IconData getProductTypeIcon(int? value) {
    final type = getProductType(value);
    return type.icon;
  }
  
  /// 获取设备状态标签组件
  Widget getDeviceStatusChip(int? value) {
    final status = getDeviceStatus(value);
    final statusType = status.statusType;
    
    return _buildStatusChip(
      label: status.displayName,
      color: getDeviceStatusColor(value),
      backgroundColor: getDeviceStatusColor(value).withOpacity(0.1),
    );
  }
  
  /// 获取告警级别标签组件
  Widget getAlertLevelChip(int? value) {
    final level = getAlertLevel(value);
    final color = level.color;
    
    return _buildStatusChip(
      label: level.displayName,
      color: color,
      backgroundColor: color.withOpacity(0.1),
    );
  }
  
  /// 获取告警状态标签组件
  Widget getAlertStatusChip(int? value) {
    final status = getAlertStatus(value);
    final statusType = status.statusType;
    Color color;
    
    switch (status) {
      case AlertStatus.generated:
        color = Colors.red;
        break;
      case AlertStatus.recovered:
        color = Colors.green;
        break;
      case AlertStatus.confirmed:
        color = Colors.grey;
        break;
    }
    
    return _buildStatusChip(
      label: status.displayName,
      color: color,
      backgroundColor: color.withOpacity(0.1),
    );
  }
  
  /// 内部方法：构建状态标签
  Widget _buildStatusChip({
    required String label,
    required Color color,
    required Color backgroundColor,
  }) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: BorderRadius.circular(4),
      ),
      child: Text(
        label,
        style: TextStyle(
          color: color,
          fontSize: 12,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }
  
  @override
  void onInit() {
    super.onInit();
    debugPrint('EnumService initialized');
  }
} 